package com.example.androidtest1;

import android.content.Intent;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.androidtest1.ui.login.LoginActivity;

import java.text.SimpleDateFormat;
import java.util.Date;


public class MainActivity extends AppCompatActivity {
    TextView mainTv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取textView控件
        mainTv = findViewById(R.id.ShowTime);
        new TimeThread().start();//启动线程

    }

    //写一个新的线程每隔一秒发送一次消息,这样做会和系统时间相差1秒
    public class TimeThread extends Thread{
        @Override
        public void run() {
            super.run();
            do{
                try {
                    Thread.sleep(1000);
                    Message msg = new Message();
                    msg.what = 1;
                    handler.sendMessage(msg);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }while (true);

        }
    }
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    mainTv.setText(new SimpleDateFormat("HH:mm:ss").format(new Date(System.currentTimeMillis())));
                    break;
            }
            return false;
        }
    });

    public void LifeCycleClick(View view) {//点击lifecycle按钮跳转到lifecycle页面
        Intent intent=new Intent(this,LifeCycle.class);
        startActivity(intent);
    }

    public void LoginClick(View view) {
        Intent intent=new Intent(this, SharedPreferenceTest.class);
        startActivity(intent);
    }
    public void playdian(View view) {
        Intent intent=new Intent(this,MainActivityProject.class);
        startActivity(intent);
    }
    public void CalculatorClick(View view) {
        Intent intent=new Intent(this, Calculator.class);
        startActivity(intent);
    }
}


